package com.iasku.loadimg;

import java.io.File;
import java.io.InputStream;
import java.lang.ref.SoftReference;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;

import com.iasku.constant.Constant;

public class MyImageLoader {
	private HashMap<String, SoftReference<Bitmap>> caches;
	private BitmapService bitmapService;
	private Context mContext;
	private String saveDir;
	private int newBmpWidth;
	private int newBmpHeight;
	
	
	public MyImageLoader(Context mContext){
		this.mContext = mContext;
		this.caches = new HashMap<String, SoftReference<Bitmap>>();
		this.bitmapService = new BitmapService();
	}


	public int getNewBmpHeight() {
		return newBmpHeight;
	}



	public void setNewBmpHeight(int newBmpHeight) {
		this.newBmpHeight = newBmpHeight;
	}



	public String getSaveDir() {
		return saveDir;
	}

	public void setSaveDir(String saveDir) {
		this.saveDir = saveDir;
	}

	public int getNewBmpWidth() {
		return newBmpWidth;
	}

	public void setNewBmpWidth(int newBmpWidth) {
		this.newBmpWidth = newBmpWidth;
	}

	public Bitmap loadHeadImage(String loadPath) {
        if (caches.containsKey(loadPath)) {
            Bitmap bm = caches.get(loadPath).get();
            if (bm != null) {
                return bm;
            } else {
                caches.remove(loadPath);
            }
        }
        String savePath = mContext.getCacheDir().getAbsolutePath() + saveDir + loadPath;
        File file = new File(savePath);
        Bitmap temp = null;
        if (file.exists()) {
            Bitmap bmp = bitmapService.getBitmap(savePath);
            return bmp;
        } else {
            try {
                URL url = new URL(Constant.HEAD_IMG_URL + loadPath);
                URLConnection connection = url.openConnection();
                InputStream in = connection.getInputStream();
                temp = bitmapService.getBitmap(in);
                if (newBmpWidth == 0) {
                    newBmpWidth = temp.getWidth();
                }
                Bitmap bmp = null;
                if (newBmpHeight == 0) {
                    bmp = bitmapService.getBitmap(temp, newBmpWidth).copy(Config.ARGB_8888, false);
                } else {
                    bmp = bitmapService.getBitmap(temp, newBmpWidth, newBmpHeight).copy(Config.ARGB_8888, false);
                }
                caches.put(loadPath, new SoftReference<Bitmap>(bmp));
                bitmapService.save(bmp, savePath);
                if (temp != null)
                    temp.recycle();
                return bmp;
            } catch (Exception e) {
                return null;
            }
        }
    }
	
	public Bitmap loadSubmitImage(String loadPath){
	       
        if(caches.containsKey(loadPath)){
            Bitmap bm = caches.get(loadPath).get();
            if (bm != null) {
                return bm;
            } else {
                caches.remove(loadPath);
            }
        }
        String savePath = mContext.getFilesDir().getAbsolutePath()
                + saveDir + loadPath.substring(loadPath.lastIndexOf("/") + 1);
        File file = new File(savePath);
        Bitmap temp = null;
        if(file.exists()){
            Bitmap bmp = bitmapService.getBitmap(savePath);
            return bmp;
        }else{
            try{
                URL url = new URL(loadPath);
                URLConnection connection = url.openConnection();
                InputStream in = connection.getInputStream();
                temp = bitmapService.getBitmap(in);
                if(newBmpWidth == 0){
                    newBmpWidth = temp.getWidth();
                }
                Bitmap bmp = null;
                if(newBmpHeight==0){
                    bmp = bitmapService.getBitmap(temp, newBmpWidth).copy(Config.ARGB_8888, false);
                }else{
                    bmp = bitmapService.getBitmap(temp, newBmpWidth, newBmpHeight).copy(Config.ARGB_8888, false);
                }
                caches.put(loadPath,new SoftReference<Bitmap>(bmp));
                bitmapService.save(bmp, savePath);
                if(temp != null)
                    temp.recycle(); 
                return bmp;
            }catch(Exception e){
                return null;
            }
        }
    }
	
}
